package com.syblood.app.camera.watermark.render;

import android.opengl.GLES11Ext;
import android.opengl.GLES30;
import android.util.Log;

import com.syblood.app.camera.watermark.utils.GlesUtil;
import com.syblood.app.utils.ConstantsUtils;

/**
 * @CreateTime : 2022/11/15
 * @Author : JiChangkong
 * @Description :
 */
public class CameraRenderDrawer extends BaseRenderDrawer {
    private static final String TAG = ConstantsUtils.TAG_HEAD + CameraRenderDrawer.class.getSimpleName();

    private int mCameraType;
    private int mCameraTextureId1;
    private int mCameraTextureId2;
    private int av_Position;
    private int af_Position;
    private int sTexture;
    private int mInputTextureId;
    private int mOutputTextureId;

    public CameraRenderDrawer(int mCameraTexture1) {
        this.mCameraType = mCameraTexture1;
    }

    @Override
    protected void onCreated() {
    }

    @Override
    protected void onChanged(int width, int height) {
        mOutputTextureId = GlesUtil.createFrameTexture(width, height);

        av_Position = GLES30.glGetAttribLocation(mProgram, "av_Position");
        af_Position = GLES30.glGetAttribLocation(mProgram, "af_Position");
        sTexture = GLES30.glGetUniformLocation(mProgram, "s_Texture");
    }

//    @Override
//    public void draw(long timestamp, float[] transformMatrix) {
//        if(mCameraType == ConstantsUtils.CAMERA_TYPE_BACK){
//            super.draw(timestamp,transformMatrix);
//        }else {
//            useProgram();
//
//            viewPort(0, 0, width, height);
//            Log.d(TAG, "draw width = " + width + " , height = " + height);
//            GLES30.glDisable(GLES30.GL_DEPTH_TEST);
//            GLES30.glEnable(GLES30.GL_BLEND);
//            GLES30.glBlendFunc(GLES30.GL_SRC_COLOR, GLES30.GL_DST_ALPHA);
//            onDraw();
//            GLES30.glDisable(GLES30.GL_BLEND);
//        }
//
//
//    }

    @Override
    protected void onDraw() {
        if (mInputTextureId == 0 || mOutputTextureId == 0) {
            return;
        }

        drawCamera(ConstantsUtils.CAMERA_TYPE_BACK);
        drawCamera(ConstantsUtils.CAMERA_TYPE_FRONT);
    }

    private void drawCamera(int cameraType) {
        GLES30.glEnableVertexAttribArray(av_Position);
        GLES30.glEnableVertexAttribArray(af_Position);
        //GLES30.glVertexAttribPointer(av_Position, CoordsPerVertexCount, GLES30.GL_FLOAT, false, VertexStride, mVertexBuffer);
        if (cameraType == ConstantsUtils.CAMERA_TYPE_BACK) {
            GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVertexBufferId);
        } else {
            GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mSmallVertexBufferId);
        }
        GLES30.glVertexAttribPointer(av_Position, CoordsPerVertexCount, GLES30.GL_FLOAT, false, 0, 0);
        if (cameraType == ConstantsUtils.CAMERA_TYPE_BACK) {
            GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mBackTextureBufferId);
        } else {
            GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mFrontTextureBufferId);
        }
        GLES30.glVertexAttribPointer(af_Position, CoordsPerTextureCount, GLES30.GL_FLOAT, false, 0, 0);
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);

        if (cameraType == ConstantsUtils.CAMERA_TYPE_BACK) {
            bindTexture(mCameraTextureId1);
        }else {
            bindTexture(mCameraTextureId2);
        }
        //绘制 GLES30.GL_TRIANGLE_STRIP:复用坐标
        GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, VertexCount);
        unBindTexure();
        GLES30.glDisableVertexAttribArray(av_Position);
        GLES30.glDisableVertexAttribArray(af_Position);
    }

    private void bindTexture(int textureId) {
        Log.d(TAG, "bindTexture textureId = " + textureId);
        GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
        GLES30.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId);
        GLES30.glUniform1i(sTexture, 0);
    }

    private void unBindTexure() {
        GLES30.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
    }

    @Override
    public void setInputTextureId(int textureId) {
        mInputTextureId = textureId;
    }

    public void setCameraTextureId(int cameraTextureId1,int cameraTextureId2) {
        this.mCameraTextureId1 = cameraTextureId1;
        this.mCameraTextureId2 = cameraTextureId2;
    }

    @Override
    public int getOutputTextureId() {
        return mOutputTextureId;
    }

    @Override
    protected String getVertexSource() {
        final String source = "attribute vec4 av_Position; " +
                "attribute vec2 af_Position; " +
                "varying vec2 v_texPo; " +
                "void main() { " +
                "    v_texPo = af_Position; " +
                "    gl_Position = av_Position; " +
                "}";
        return source;
    }

    @Override
    protected String getFragmentSource() {
        final String source = "#extension GL_OES_EGL_image_external : require \n" +
                "precision mediump float; " +
                "varying vec2 v_texPo; " +
                "uniform samplerExternalOES s_Texture; " +
                "void main() { " +
                "   gl_FragColor = texture2D(s_Texture, v_texPo); " +
                "} ";
        return source;
    }

    private int mDrawCameraType = ConstantsUtils.CAMERA_TYPE_BACK;

    public void setDrawCameraType(int cameraIndex) {
        mDrawCameraType = cameraIndex;
    }
}
